package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.domain.IntegralUserShippingAddressDO;
import com.jinzhi.jzweb.service.IntegralStoreCommodityService;
import com.jinzhi.jzweb.service.IntegralUserShippingAddressService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.IntegralUserOrderDO;
import com.jinzhi.jzweb.service.IntegralUserOrderService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 用户订单表
 * </pre>
 * <small> 2023-02-16 09:04:34 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/integralUserOrder")
public class IntegralUserOrderController extends BaseController {
	@Autowired
	private IntegralUserOrderService integralUserOrderService;

	@Autowired
	private IntegralStoreCommodityService integralStoreCommodityService;

	@Autowired
	private IntegralUserShippingAddressService integralUserShippingAddressService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:integralUserOrder:integralUserOrder")
	String IntegralUserOrder(){
	    return "jzweb/integralUserOrder/integralUserOrder";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:integralUserOrder:integralUserOrder")
	public Result<Page<IntegralUserOrderDO>> list(IntegralUserOrderDO integralUserOrderDTO){
		EntityWrapper entityWrapper =new EntityWrapper();
		if (!"".equals(integralUserOrderDTO.getType()) && integralUserOrderDTO.getType() !=null){
			entityWrapper.eq("type",integralUserOrderDTO.getType());
		}
		if (!"".equals(integralUserOrderDTO.getName()) && integralUserOrderDTO.getName() !=null){
			entityWrapper.eq("receiving_name",integralUserOrderDTO.getName());
		}
		entityWrapper.orderBy("type",true);
		entityWrapper.orderBy("create_time",false);
		// 查询列表数据
		Page<IntegralUserOrderDO> page = integralUserOrderService.selectPage(getPage(IntegralUserOrderDO.class),
				entityWrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				IntegralUserOrderDO obj = (IntegralUserOrderDO) page.getRecords().get(i);
				obj.setCommodityName(integralStoreCommodityService.selectById(obj.getCommodityId()).getCommodityName());
				obj.setAddress(obj.getProvince() + "" +obj.getCity()
						+ "" + obj.getDistrict() + ""+obj.getAddress());
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:integralUserOrder:add")
	String add(){
	    return "jzweb/integralUserOrder/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:integralUserOrder:edit")
	String edit(@PathVariable("id") Long id,Model model){
		IntegralUserOrderDO integralUserOrder = integralUserOrderService.selectById(id);
		model.addAttribute("integralUserOrder", integralUserOrder);
	    return "jzweb/integralUserOrder/edit";
	}

	@Log("添加用户订单表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:integralUserOrder:add")
	public Result<String> save( IntegralUserOrderDO integralUserOrder){
		integralUserOrderService.insert(integralUserOrder);
        return Result.ok();
	}
	
	@Log("修改用户订单表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:integralUserOrder:edit")
	public Result<String>  update( IntegralUserOrderDO integralUserOrder){

		integralUserOrder.setUpdateTime(new Date()); //修改时间
		integralUserOrder.setDeliverDate(new Date()); //发货时间
		integralUserOrder.setConsigner(this.getUser().getName()); //发货人
		integralUserOrder.setType("1"); //发货状态

		boolean update = integralUserOrderService.updateById(integralUserOrder);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除用户订单表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:integralUserOrder:remove")
	public Result<String>  remove( Long id){
		integralUserOrderService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除用户订单表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:integralUserOrder:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		integralUserOrderService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
